package com.lyubia.shop.manager.controller;

import com.lyubia.shop.common.result.BaseResult;
import com.lyubia.shop.manager.pojo.Brand;
import com.lyubia.shop.manager.pojo.Goods;
import com.lyubia.shop.manager.pojo.GoodsCategory;
import com.lyubia.shop.manager.query.GoodsQuery;
import com.lyubia.shop.manager.service.BrandService;
import com.lyubia.shop.manager.service.GoodsCategoryService;
import com.lyubia.shop.manager.service.GoodsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    @Resource
    private GoodsCategoryService goodsCategoryService;

    @Resource
    private BrandService brandService;

    @GetMapping("/index")
    public String index(Model model) {
        List<Brand> brandList = brandService.selectAllBrand();
        List<GoodsCategory> goodsCategoryList = goodsCategoryService.selectAllCategory();
        model.addAttribute("brandList", brandList);
        model.addAttribute("goodsCategoryList", goodsCategoryList);
        return "goods/goods_list";
    }

    /**
     * 前往新增页面
     *      返回一级商品分类信息
     *      返回品牌列表
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model) {
        List<GoodsCategory> topCategoryList = goodsCategoryService.selectTopCategory();
        List<Brand> brandList = brandService.selectAllBrand();
        model.addAttribute("topCategoryList",topCategoryList);
        model.addAttribute("brandList",brandList);
        return "goods/goods_add";
    }

    /**
     * 保存商品信息
     * @param goods 商品信息
     * @return 结果集
     */
    @PostMapping("/save")
    @ResponseBody
    public BaseResult save(Goods goods) {
        return goodsService.save(goods);
    }

    /**
     * 商品信息多条件分页查询
     * @param goodsQuery 查询条件
     * @return 结果
     */
    @PostMapping("/queryGoodsByParam")
    @ResponseBody
    public BaseResult queryGoodsByParam(GoodsQuery goodsQuery) {
        return goodsService.queryGoodsByParam(goodsQuery);
    }

}
